﻿Public Class frmProductionPriceStockList
    Dim dbBase As New dbProductionPriceStock
    Dim frmCtrl As New FormControl
    Dim dsBase As DataSet
    Dim EntryID As String
    Dim copyText As String

    Private Sub frmProductionPriceStockList_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
        EntryID = ""
        dsBase = dbBase.GetBaseList(False)
        If (dsBase IsNot Nothing) Then
            dgvBase.Rows.Clear()
            For Each col As DataColumn In dsBase.Tables(0).Columns
                dgvBase.Columns.Add(col.ColumnName, col.ColumnName)
                If (col.ColumnName = "Stock" Or col.ColumnName = "OpenSO" Or col.ColumnName = "OpenPO" Or col.ColumnName = "Actual" Or col.ColumnName = "MinStock" Or _
                    col.ColumnName = "Euro" Or col.ColumnName = "USD" Or col.ColumnName = "IDR") Then
                    dgvBase.Columns(col.ColumnName).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
                    dgvBase.Columns(col.ColumnName).DefaultCellStyle.Format = "N0"
                    dgvBase.Columns(col.ColumnName).AutoSizeMode = DataGridViewAutoSizeColumnMode.None
                    dgvBase.Columns(col.ColumnName).Width = 75
                End If
                If (col.ColumnName = "Description") Then
                    dgvBase.Columns(col.ColumnName).AutoSizeMode = DataGridViewAutoSizeColumnMode.None
                    dgvBase.Columns(col.ColumnName).Width = 150
                    dgvBase.Columns(col.ColumnName).DefaultCellStyle.WrapMode = DataGridViewTriState.True
                End If
                If (col.ColumnName = "LastUpdate") Then
                    dgvBase.Columns(col.ColumnName).DefaultCellStyle.Format = "dd MMM yyyy"
                End If
                If (col.ColumnName = "Note") Then
                    dgvBase.Columns(col.ColumnName).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
                End If
                If (col.DataType.ToString() = "System.DateTime") Then
                    dgvBase.Columns(col.ColumnName).SortMode = DataGridViewColumnSortMode.NotSortable
                End If
                'MessageBox.Show(col.ColumnName + " = " + col.DataType.ToString())
            Next col
            For Each row As DataRow In dsBase.Tables(0).Rows
                dgvBase.Rows.Add(row.ItemArray)
            Next row
            'dgvBase.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
        End If
        ToolbarHeader1.Iniliaze("LIST : Production Price And Stock List", Me, Nothing, Nothing, "Code")
        ToolbarFind1.Initialize_Tools(dsBase, dgvBase)
        ToolbarHeader1.ActionCtrl(False, False, False, False, False, False, False, True, True)
    End Sub

    Private Sub tsRefresh_Click(ByVal sender As Object) Handles ToolbarHeader1.tsRefreshClick
        frmCtrl.openChild(New frmProductionPriceStockList, Me.ParentForm)
    End Sub

    Private Sub dgvBase_RowsAdded(sender As Object, e As DataGridViewRowsAddedEventArgs) Handles dgvBase.RowsAdded
        If (Convert.ToDecimal(dgvBase.Rows(e.RowIndex).Cells("Actual").Value) < Convert.ToDecimal(dgvBase.Rows(e.RowIndex).Cells("MinStock").Value)) Then
            dgvBase.Rows(e.RowIndex).Cells("Actual").Style.BackColor = Color.LightPink
        Else : dgvBase.Rows(e.RowIndex).Cells("Actual").Style.BackColor = Color.LightGreen
        End If
    End Sub

    Private Sub dgvBase_CellEnter(sender As Object, e As DataGridViewCellEventArgs) Handles dgvBase.CellEnter
        EntryID = dgvBase.Rows(e.RowIndex).Cells("Code").Value
        Dim x As Array = EntryID.Split("-")
        Dim groups As String = x(0).ToString()
        Dim code As String = x(1).ToString()
        dgvPOList.DataSource = dbBase.GetPOList(groups, code).Tables(0)
        dgvSOList.DataSource = dbBase.GetSOList(groups, code).Tables(0)
    End Sub

    Private Sub dgvBase_Sorted(sender As Object, e As EventArgs) Handles dgvBase.Sorted
        If (dgvBase.Columns.Count > 0) Then
            ToolbarFind1.GridTarget_SortChange()
        End If
    End Sub

    Private Sub dgvBase_CellMouseDown(sender As Object, e As DataGridViewCellMouseEventArgs) Handles dgvBase.CellMouseDown
        If e.Button = Windows.Forms.MouseButtons.Right AndAlso e.RowIndex >= 0 Then
            dgvBase.Rows(e.RowIndex).Selected = True
            copyText = dgvBase.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString()
            dgvBase.ClearSelection()
            dgvBase.CurrentCell = dgvBase.Rows(e.RowIndex).Cells(0)
            dgvBase.Rows(e.RowIndex).Selected = True
        End If
    End Sub

    Private Sub GridCMS_ItemClicked(sender As Object, e As ToolStripItemClickedEventArgs) Handles GridCMS.ItemClicked
        Select Case e.ClickedItem.Text
            Case "Copy"
                Clipboard.SetText(copyText)
        End Select
    End Sub

    Private Sub frmProductionPriceStockList_SizeChanged(sender As Object, e As EventArgs) Handles MyBase.SizeChanged
        Dim fW As Integer = Me.Width
        pnlPO.Width = (fW / 2)
        'pnlSO.Location = New Point((fW / 2), pnlSO.Location.Y)
        pnlSO.Width = (fW / 2)
    End Sub

    Private Sub cbViewAll_CheckedChanged(sender As Object, e As EventArgs) Handles cbViewAll.CheckedChanged
        If (cbViewAll.Checked) Then
            pnlFiltered.Visible = False
        Else : pnlFiltered.Visible = True
        End If
    End Sub

    Private Sub doFilter()
        If (cbViewMinusOnly.Checked) Then
            dsBase = dbBase.GetBaseList(True)
        Else
            dsBase = dbBase.GetBaseList(False)
        End If
        dgvBase.Rows.Clear()
        For Each row As DataRow In dsBase.Tables(0).Rows
            dgvBase.Rows.Add(row.ItemArray)
        Next row
        ToolbarFind1.Initialize_Tools(dsBase, dgvBase)
    End Sub

    Private Sub cbViewMinusOnly_CheckedChanged(sender As Object, e As EventArgs) Handles cbViewMinusOnly.CheckedChanged
        doFilter()
    End Sub

    Private Sub tsRefresh_Click(sender As ToolbarHeader) Handles ToolbarHeader1.tsRefreshClick

    End Sub
End Class